package com.lin.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lin.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lin.gulimall.product.entity.AttrEntity;
import com.lin.gulimall.product.service.*;
import com.lin.gulimall.product.vo.AttrGroupRelationVo;
import com.lin.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.lin.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lin.gulimall.product.entity.AttrGroupEntity;
import com.lin.common.utils.PageUtils;
import com.lin.common.utils.R;



/**
 * 属性分组
 *
 * @author lin
 * @email 331025994@gmail.com
 * @date 2022-01-05 19:35:30
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Autowired
    private CategoryBrandRelationService brandRelationService;

    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    //   /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){

        List<AttrEntity> data  = attrService.getRelationAttr(attrGroupId);

        return R.ok().put("data",data);

    }

    //   /product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R noAttrRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrgroupId") Long attrGroupId){

        PageUtils page  = attrService.getNoRelationAttr(params,attrGroupId);

        return R.ok().put("page",page);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R relationDelete(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);

        return R.ok();
    }

    @PostMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:delete")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){

        List<AttrAttrgroupRelationEntity> list = vos.stream().map(attrGroup -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrGroup, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());

        relationService.saveBatch(list);

        return R.ok();
    }

    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithattrs(@PathVariable("catelogId") Long catelogId){

        //1、查出当前分类下的所有属性分组

        //2、查出每个属性分组的所有属性。
        List<AttrGroupWithAttrsVo> data = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",data);
    }

}
